package @{package};

import java.util.*;
import org.json.*;

import com.yonyou.uap.um.context.util.*;
import com.yonyou.uap.um.controller.*;
import com.yonyou.uap.um.gateway.exception.*;
import com.yonyou.uap.um.gateway.service.*;


public class @{controllername} extends AbstractUMController
{ 
     @Override
    public String load(String args) throws Exception {
        // get all params from args. This line is MUST!
		Map map = UmContextUtil.transJsonToMap(new JSONObject(args));
		JSONObject jsonObj = new JSONObject(args);
        // set service id
        String serviceid = "demoService";
        // get some key values from json
        String appid = jsonObj.getString("appid");
        String userid = jsonObj.getString("user");
        String groupid = jsonObj.getString("groupid");
        String demoParams = jsonObj.getString("demoParams");
        // set params values in map for GatewayService
        map.put("userid", userid);
        map.put("groupid", groupid);
        map.put("params", demoParams);
        // invoke gateway service
        IGatewayService service = GatewayServiceFactory.findGatewayService(appid, serviceid, map);
        Object returnObj = service.doService();
        // process return Object with your own ways for example
        List list = (List)returnObj;
        JSONArray jsonArray = new JSONArray();
        UmContextUtil.entitiesMappingToArray(list, jsonArray);
        String resultStr = new JSONObject().put("demos", jsonArray).toString();
        return resultStr;
    }

    @Override
    public String save(String args) throws Exception {
        // get all params from args. This line is MUST!
		Map map = UmContextUtil.transJsonToMap(new JSONObject(args));
		JSONObject jsonObj = new JSONObject(args);
        JSONObject data = new JSONObject(jsonObj.getString("data"));
        // set service id
        String serviceid = "demoService";
        // get some key values from json
        String appid = jsonObj.getString("appid");
        String userid = jsonObj.getString("user");
        String groupid = jsonObj.getString("groupid");
        // convert json to MBE. For example:
        Object entity = new Object();
        UmContextUtil.jsonMappingToEntity(entity,data);
        // set params values in map for GatewayService
        map.put("entity", entity);
        // invoke gateway service
        IGatewayService service = GatewayServiceFactory.findGatewayService(appid, serviceid, map);
        Object returnObj = service.doService();
        // process return Object with your own ways for example
        String resultStr = returnObj.toString();
        return resultStr;
        
    }

}